package com.travel.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.mapper.UserMapper;
import com.travel.pojo.dto.PageDTO;
import com.travel.pojo.dto.UserDTO;
import com.travel.pojo.dto.UserQueryDTO;
import com.travel.pojo.entity.User;
import com.travel.pojo.vo.UserVO;
import com.travel.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper userMapper;

    @Override
    public PageDTO<UserVO> pageListUser(UserQueryDTO userQueryDTO) {
        // 参数提取
        String name = userQueryDTO.getUsername();
        String phone = userQueryDTO.getPhone();
        Integer gender = userQueryDTO.getGender();
        String ipLocation = userQueryDTO.getIpLocation();
        BigDecimal maxAge = userQueryDTO.getMaxAge();
        BigDecimal minAge = userQueryDTO.getMinAge();
        String userPermission = userQueryDTO.getUserPermission();

        // 分页查询
        Page<User> page = Page.of(userQueryDTO.getPage(), userQueryDTO.getPageSize());
        Page<User> pageUser = lambdaQuery()
                .like(name != null, User::getUsername, name)
                .eq(phone != null, User::getPhone, phone)
                .eq(gender != null, User::getGender, gender)
                .eq(ipLocation != null, User::getIpLocation, ipLocation)
                .eq(userPermission != null, User::getUserPermission, userPermission)
                .le(maxAge != null, User::getAge, maxAge)
                .gt(minAge != null, User::getAge, minAge)
                .page(page);

        // 转换为 VO 列表（保持不变）
        List<UserVO> userVOList = pageUser.getRecords().stream()
                .map(user -> BeanUtil.toBean(user, UserVO.class))
                .collect(Collectors.toList());

        // 构建 PageDTO（优化：使用构建器或无参构造+set方法均可）
        return PageDTO.<UserVO>builder()
                .total((int) pageUser.getTotal())
                .pages((int) pageUser.getPages())
                .list(userVOList)
                .build();
        // 或使用无参构造+set方法（与原代码等效）：
        // PageDTO<UserVO> userPageDTO = new PageDTO<>();
        // userPageDTO.setTotal((int) pageUser.getTotal());
        // userPageDTO.setPages((int) pageUser.getPages());
        // userPageDTO.setList(userVOList);
        // return userPageDTO;
    }

    @Override
    public void addUser(UserDTO userDTO) {
        User user = BeanUtil.copyProperties(userDTO, User.class);
        user.setRegisterTime(LocalDateTime.now());
        userMapper.insert(user);  // 或直接使用 save(user)
    }

    @Override
    public void updateUser(Long id, UserDTO userDTO) {
        LambdaUpdateWrapper<User> wrapper = Wrappers.lambdaUpdate(User.class)
                .eq(User::getId, id)
                .set(userDTO.getAge() != null, User::getAge, userDTO.getAge())
                .set(userDTO.getGender() != null, User::getGender, userDTO.getGender())
                .set(userDTO.getUsername() != null, User::getUsername, userDTO.getUsername())
                .set(userDTO.getPhone() != null, User::getPhone, userDTO.getPhone())
                .set(userDTO.getPassword() != null, User::getPassword, userDTO.getPassword());
        userMapper.update(null, wrapper);  // 或使用 update(wrapper)
    }
}